#!/usr/bin/env python

# encoding: utf-8

'''
 * Create File __init__.py
 * Created by leixu on 2017/9/18
 * IDE PyCharm
'''
from luobocrawler.holder import SessionHolder
from selenium.webdriver.common.keys import Keys
import time


class CustomerSessionHolder(SessionHolder):
    def __init__(self):
        super().__init__()

    def start_session(self):
        return self.driver


class LinkedinSessionHolder(SessionHolder):
    def __init__(self):
        super(LinkedinSessionHolder, self).__init__()

    def start_session(self):
        return self.driver

    def login(self, config={"username": "zhouxy0626@163.com", "password": "!qaz2wsx3edc"}):
        driver = self.driver
        driver.get("https://www.linkedin.com/")
        driver.implicitly_wait(1)
        driver.find_element_by_id("login-email").send_keys(config["username"])
        driver.find_element_by_id("login-password").send_keys(config["password"], Keys.RETURN)
        driver.implicitly_wait(1)

    def search(self, q=" "):
        driver = self.driver
        driver.find_element_by_xpath("//artdeco-typeahead-input[@id]/input[@placeholder='搜索']").send_keys(q, Keys.RETURN)
        driver.implicitly_wait(1)

    def go_to_friends_list(self):
        driver = self.driver
        driver.get("http://www.linkedin.com/mynetwork/invite-connect/connections/")
        driver.implicitly_wait(1)


class FaceBookSessionHolder(SessionHolder):
    def __init__(self):
        super(FaceBookSessionHolder, self).__init__()

    def start_session(self, then=None):
        return self.driver

    def login(self):
        driver = self.driver
        driver.get("https://m.facebook.com/")
        driver.implicitly_wait(5)
        driver.find_element_by_id("m_login_email").send_keys("8615720688739")
        driver.find_element_by_id("m_login_password").send_keys("Xx349106576", Keys.RETURN)
        driver.implicitly_wait(10)
        try:
            driver.find_element_by_xpath("//div/button[@value='OK']").click()
        except:
            pass
        finally:
            time.sleep(5)
            # driver.save_screenshot("screenshot.png")

    def go_homepage(self):
        driver = self.driver
        driver.get("https://m.facebook.com/")
        # driver.save_screenshot("screenshot.png")


if __name__ == "__main__":
    _session_holder = FaceBookSessionHolder()
    _session_holder.chose_driver_type(type="chrome")
    _session_holder.start_session()
